﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace AutoIt_Elements.GUI
{
	public partial class ModuleManagementForm : Form
	{
		public ModuleManagementForm()
		{
			InitializeComponent();
		}

		private void btnAddModule_Click(object sender, EventArgs e)
		{
			string ModuleFileName = string.Empty;
			using(OpenFileDialog ofdSelectModuleFile = new OpenFileDialog())
			{
				ofdSelectModuleFile.Filter = "DLL-Files (*.dll)|*.dll";
				ofdSelectModuleFile.Multiselect = false;
				if(ofdSelectModuleFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
					ModuleFileName = ofdSelectModuleFile.FileName;
			}

			if((ModuleFileName != string.Empty) && File.Exists(ModuleFileName))
			{
				Assembly asm = Assembly.LoadFile(ModuleFileName);
				Type[] asmTypes = asm.GetTypes();

				if(asmTypes.Length > 0)
				{
					List<Type> possibleModules = new List<Type>();
					foreach(Type tmpAsmType in asmTypes)
					{
						if((typeof(AutoIt_IDE.Core.ModuleBase).IsAssignableFrom(tmpAsmType))
							|| (typeof(AutoIt_IDE.Core.GuiModuleBase).IsAssignableFrom(tmpAsmType)))
						{
							possibleModules.Add(tmpAsmType);
						}
					}

					if(possibleModules.Count > 0)
					{
						foreach(Type mod in possibleModules)
						{
							if(typeof(AutoIt_IDE.Core.ModuleBase).IsAssignableFrom(mod))
							{
								AutoIt_IDE.Core.ModuleBase tmpMod = (AutoIt_IDE.Core.ModuleBase)Activator.CreateInstance(mod);
							}
							else if(typeof(AutoIt_IDE.Core.GuiModuleBase).IsAssignableFrom(mod))
							{
								AutoIt_IDE.Core.GuiModuleBase tmpMod = (AutoIt_IDE.Core.GuiModuleBase)Activator.CreateInstance(mod);
							}
						}
					}
				}
			}
		}

		private void btnRemoveModule_Click(object sender, EventArgs e)
		{
			//TODO check if module is needed by other modules and request confirmation if so (dependent modules will be at least deactivated)
		}

		private void LoadModuleDatabase()
		{
			//TODO load module database, check if file is still okay via checksum (SHA1)
		}

		private void ActivateModule(string ModuleChecksum)
		{
			//TODO check if module given by the checksum can be activated (dependencies etc.)
		}

		private class ModuleData
		{
			public string Sha1Checksum
			{
				get;
				private set;
			}

			public string ModuleName
			{
				get;
				private set;
			}

			public string ModuleVersion
			{
				get;
				private set;
			}

			public string ModuleFilePath
			{
				get;
				private set;
			}

			public string ModuleAuthor
			{
				get;
				private set;
			}

			public bool IsActivated
			{
				get;
				private set;
			}

			public ModuleData(
				string Sha1Checksum,
				string ModuleName,
				string ModuleVersion,
				string ModuleFilePath,
				string ModuleAuthor,
				bool IsActivated)
			{
				this.Sha1Checksum = Sha1Checksum;
				this.ModuleName = ModuleName;
				this.ModuleVersion = ModuleVersion;
				this.ModuleFilePath = ModuleFilePath;
				this.ModuleAuthor = ModuleAuthor;
				this.IsActivated = IsActivated;
			}
		}
	}
}
